<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	class Manage_excel extends CI_Controller{

		public function __construct(){
			parent::__construct();
			$this->load->library('session');
			$this->load->library('form_validation');
			$this->load->library('cismarty');
			$this->load->model("apanel/template");
			$this->load->model("apanel/db_product");
			//$this->load->model("apanel/db_news");
			$this->load->library('pagination');
		}
		
		public function index(){
			//== template
			redirect('manage_excel/excel_add');
		}
		
	
		
		
	
		
	public function excel_add(){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			
			if($this->input->post('bntSubmit') != null){
		
			$this->db->where("name",'huawei');
				$res = $this->db->get('mobile_customer');
				$row = $res->row();
			
					
				 $dom=$_FILES['file']['tmp_name'] ;     
					
			       
				$FILE = fopen($dom,"r");
				if($FILE!=""){
				$DATA = fgetcsv($FILE, 1000, ",");
				$i=1;
	
					do
						{
							if ($i == 1){
							$DATA = fgetcsv($FILE, 1000, ",");
							$i++;
						}else{
		 
							$this->db->set('category_pkid',iconv('TIS-620', 'UTF-8',$DATA[0]));
							$this->db->set('customer_pkid',$row->pkid);
							$this->db->set('code',iconv('TIS-620', 'UTF-8',$DATA[1]));
							$this->db->set('name_en',iconv('TIS-620', 'UTF-8',$DATA[2]));
							$this->db->set('name_zh_cn',iconv('TIS-620', 'UTF-8',$DATA[3]));
							$this->db->set('name_th',iconv('TIS-620', 'UTF-8',$DATA[4]));
							$this->db->set('description_en',iconv('TIS-620', 'UTF-8',$DATA[5]));
							$this->db->set('description_zh_cn',iconv('TIS-620', 'UTF-8',$DATA[6]));
							$this->db->set('description_th',iconv('TIS-620', 'UTF-8',$DATA[7]));
							$this->db->set('price',iconv('TIS-620', 'UTF-8',$DATA[8]));
							$this->db->set('warranty_period',iconv('TIS-620', 'UTF-8',$DATA[9]));
							$this->db->set('status',"1");
							$this->db->set('created_by',$this->session->userdata("sess_username"));
							$this->db->set('created_date',date("Y-m-d H:i:s"));
							$this->db->insert('mobile_product_item');
		 
	   
							//if($res) redirect('manage_product/product_list/success');
							//else  redirect('manage_product/product_list/fail');
							
							$DATA = fgetcsv($FILE, 1000, ",");
							$i++;
						}
					}while ( !feof( $FILE ) );
			
				}
		////////////////////////////////////////////////////////////////////////////////////////
		
				$dom1=$_FILES['file1']['tmp_name'] ;     
					
			      
				$FILE1 = fopen($dom1,"r");
				if($FILE1!=""){
				$DATA1 = fgetcsv($FILE1, 1000, ",");
				$z=1;
	
					do
					{
						if ($z == 1){
						$DATA1 = fgetcsv($FILE1, 1000, ",");
						$z++;
					}else{
     
   	 					$this->db->set('serial_no',iconv('TIS-620', 'UTF-8',$DATA1[0]));
						$this->db->set('product_pkid',iconv('TIS-620', 'UTF-8',$DATA1[1]));
						$this->db->set('create_date',date("Y-m-d H:i:s"));
						$this->db->set('status',"1");
        				$this->db->insert('mobile_serial');

						$DATA1 = fgetcsv($FILE1, 1000, ",");
						$z++;
					}
				}while ( !feof( $FILE1 ) );
			}
					if($this->input->post('serial')!=""){
						$sr = array(
								"serial_no"=>$this->input->post('serial'),
								"product_pkid"=>$this->input->post('product'),
								"create_date"=>date("Y-m-d H:i:s"),
								"status"=>"1",
						);
							$this->db->insert('mobile_serial',$sr);
					}
			
				redirect('manage_product/product_list/success');
				
				}
				
			$this->cismarty->assign("cate",$this->db_product->show_cate());	
			$this->cismarty->assign("pro",$this->db_product->show_pro());	
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(6,2));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("form_open",form_open_multipart("manage_excel/excel_add"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->display('apanel/product/excel.html');
		}
		
		public function cate_no($id,$group=""){
			//echo "group=".$group;
				
			if($id == "") $id=0;
			$this->db->where('category_pkid',$id);

			$this->db->order_by("pkid","asc");
			$res = $this->db->get('mobile_product_item');
			$str ='<select name="product">';
			$str .= '<option value="">=select Product=</option>';
			foreach ($res->result_array() as $P){
				if($group == $P['pkid']){
					$check = 'selected="selected"';
				}else {
					$check = "";
				}
				$str .='<option value="'.$P['pkid'].'" '.$check.'>'.$P['name_en'].'</option>';
				
			}
			$str .="</select>";
			echo $str;
		}
		
	
		
		
	}
?>